package org.millioncall.yueyoga.admin.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.OrderDetail;
import org.millioncall.yueyoga.admin.service.OrderDetailService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class OrderDetailAction {
	private OrderDetailService orderDetailService;

	public OrderDetailService getOrderDetailService() {
		return orderDetailService;
	}

	public void setOrderDetailService(OrderDetailService orderDetailService) {
		this.orderDetailService = orderDetailService;
	}
	
	/** 查找订单明细，并分页 */
	@RequestMapping(value="/ajaxgetorderdetailinpage.do")
	public void findOrderDetail(HttpServletRequest request, HttpServletResponse response){
		String msg = "";
		int start = 0;
		int pageSize = 20;
		
		String strStart = request.getParameter("start");
		try {
			start = Integer.parseInt(strStart);
		} catch (NumberFormatException e) {
			start = 0;
		}
		
		String strLimit = request.getParameter("limit");
		try {
			pageSize = Integer.parseInt(strLimit);
		} catch (NumberFormatException e) {
			pageSize = 20;
		}
		PaginatedList<OrderDetail> p = null;
		String[] paramNames = request.getParameterValues("paramNames");
		String[] paramValues = request.getParameterValues("paramValues");
		if(paramNames != null && paramValues != null) {
			p = this.orderDetailService.findInPage(start, pageSize, paramNames, paramValues);
		}else{
			p = this.orderDetailService.findInPage(start, pageSize);
		}
		if(p == null || p.size() == 0){
			msg = "{'success':true, 'total':0, 'items':[]}";
		}else{
			StringBuffer buffer = new StringBuffer("[");
			for(OrderDetail orderDetail : p){
				buffer.append("{");
				buffer.append("'id':'" + orderDetail.getOrderDetailId() + "',");
				buffer.append("'productId':'" + orderDetail.getProduct().getProductId() + "',");
				buffer.append("'productName':'" + orderDetail.getProduct().getName() + "',");
				buffer.append("'productType':'" + orderDetail.getProduct().getType() + "',");
				buffer.append("'productCounts':'" + orderDetail.getProduct().getCounts() + "',");
				buffer.append("'productBeginTime':'" + orderDetail.getProduct().getBeginTime() + "',");
				buffer.append("'productEndTime':'" + orderDetail.getProduct().getEndTime() + "',");
				buffer.append("'productPrice':'" + orderDetail.getProduct().getPrice() + "',");
				buffer.append("'productRemarks':'" + orderDetail.getProduct().getRemarks() + "'},");
			}
			
			String str = buffer.toString();
			str = str.substring(0, str.length() - 1);
			str += "]";
			msg = "{'success':true, 'total':" + p.getPager().getCountOfTotalResults() + ", 'items':" + str + "}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	@RequestMapping(value="/ajaxgetorderdetailbyid.do")
	public void findOrderDetailById(HttpServletRequest request, HttpServletResponse response){
		String msg = "";
		
		int start = 0;
		int pageSize = 20;
		
		String strStart = request.getParameter("start");
		try {
			start = Integer.parseInt(strStart);
		} catch (NumberFormatException e) {
			start = 0;
		}
		
		String strLimit = request.getParameter("limit");
		try {
			pageSize = Integer.parseInt(strLimit);
		} catch (NumberFormatException e) {
			pageSize = 20;
		}
		
		String strOrderId = request.getParameter("orderId");
		int orderId = 0;
		if(StringUtils.isBlank(strOrderId)){
			msg = "{'success':false,'msg':'账单编号不可为空'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}else{
			try {
				orderId = Integer.parseInt(strOrderId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'账单编号格式不正确'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
			
			PaginatedList<OrderDetail> list = this.orderDetailService.findAllOrderDetail(orderId, start, pageSize);
			
			StringBuffer buffer = new StringBuffer("[");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			String strBeginTime = null;
			String strEndTime = null;
			for(OrderDetail orderDetail : list){
				buffer.append("{");
				buffer.append("'id':'" + orderDetail.getOrderDetailId() + "',");
				String strType = "";
				if(orderDetail.getProduct().getType() == 1)
					strType = "次卡";
				else if(orderDetail.getProduct().getType() == 2)
					strType = "时间卡";
				else if(orderDetail.getProduct().getType() == 3)
					strType = "年卡";
				else if(orderDetail.getProduct().getType() == 4)
					strType = "季卡";
				else if(orderDetail.getProduct().getType() == 5)
					strType = "月卡";
				else if(orderDetail.getProduct().getType() == 6)
					strType = "半月卡";
				else if(orderDetail.getProduct().getType() == 7)
					strType = "金额卡";
				buffer.append("'productId':'" + orderDetail.getProduct().getProductId() + "',");
				buffer.append("'productName':'" + orderDetail.getProduct().getName() + "',");
				buffer.append("'productType':'" + strType + "',");
				buffer.append("'productCounts':'" + orderDetail.getProduct().getCounts() + "',");
				date = orderDetail.getProduct().getBeginTime();
				strBeginTime = sdf.format(date);
				buffer.append("'productBeginTime':'" + strBeginTime + "',");
				date = orderDetail.getProduct().getEndTime();
				strEndTime = sdf.format(date);
				buffer.append("'productEndTime':'" + strEndTime + "',");
				buffer.append("'productPrice':'" + orderDetail.getProduct().getPrice() + "',");
				buffer.append("'productRemarks':'" + orderDetail.getProduct().getRemarks() + "'},");
			}
			
			String str = buffer.toString();
			str = str.substring(0, str.length() - 1);
			str += "]";
			msg = "{'success':true, 'total':" + list.size() + ", 'items':" + str + "}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	/** 查找订单明细 */
	@RequestMapping(value="findOrderDetailByOrderId.jhtml")
	public void findOrderDetailByOrderIdAndMemberId(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		
		String strOrderId = request.getParameter("orderId");
		
		int start = 0, pageSize = 5, orderId = 0;
		
		String strStart = request.getParameter("start");
		try {
			start = Integer.parseInt(strStart);
		} catch (NumberFormatException e) {
			start = 0;
		}
		
		String strLimit = request.getParameter("limit");
		try {
			pageSize = Integer.parseInt(strLimit);
		} catch (NumberFormatException e) {
			pageSize = 5;
		}
		if (start <= 0) start = 1;
		start = (start - 1) * pageSize;
		
		try {
			orderId = Integer.parseInt(strOrderId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'订单编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		PaginatedList<OrderDetail> pl = this.orderDetailService.findByOrderId(start, pageSize, map);
		
		if (pl.size() <= 0) {
			msg = "{'success':false, 'msg':'未找到数据'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		StringBuffer buffer = new StringBuffer("[");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		String strBeginTime = null;
		String strEndTime = null;
		for(OrderDetail orderDetail : pl){
			buffer.append("{");
			buffer.append("'id':'" + orderDetail.getOrderDetailId() + "',");
			String strType = "";
			if(orderDetail.getProduct().getType() == 1)
				strType = "次卡";
			else if(orderDetail.getProduct().getType() == 2)
				strType = "时间卡";
			else if(orderDetail.getProduct().getType() == 3)
				strType = "年卡";
			else if(orderDetail.getProduct().getType() == 4)
				strType = "季卡";
			else if(orderDetail.getProduct().getType() == 5)
				strType = "月卡";
			else if(orderDetail.getProduct().getType() == 6)
				strType = "半月卡";
			else if(orderDetail.getProduct().getType() == 7)
				strType = "金额卡";
			else
				strType = "普通消费";
			buffer.append("'productId':'" + orderDetail.getProduct().getProductId() + "',");
			buffer.append("'productName':'" + orderDetail.getProduct().getName() + "',");
			buffer.append("'productType':'" + strType + "',");
			buffer.append("'productCounts':'" + orderDetail.getProduct().getCounts() + "',");
			date = orderDetail.getProduct().getBeginTime();
			strBeginTime = date == null ? "" : sdf.format(date);
			buffer.append("'productBeginTime':'" + strBeginTime + "',");
			date = orderDetail.getProduct().getEndTime();
			strEndTime = date == null ? "" : sdf.format(date);
			buffer.append("'productEndTime':'" + strEndTime + "',");
			buffer.append("'productPrice':'" + orderDetail.getProduct().getPrice() + "',");
			buffer.append("'productRemarks':'" + orderDetail.getProduct().getRemarks() + "'},");
		}
		
		String str = buffer.toString();
		str = str.substring(0, str.length() - 1);
		str += "]";
		msg = "{'success':true, 'total':'" + pl.size() + "', 'totalResults':'" + pl.getPager().getCountOfTotalResults().intValue() + "', 'totalPages':'" + pl.getPager().getCountOfTotalPages() + "', 'items':" + str + "}";
		System.out.println(msg);
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	
}
